package day13.answer;

import com.sun.xml.internal.ws.api.message.MessageWritable;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

//c.设计一个用户对象包含(手机号码(String),姓名(String),密码(String)
//套餐对象,消费金额(double),当前余额(double),真实通话时长(int),真实短信条数(int),
//真实使用流量(int)) 要求对类进行封装
public class User implements Serializable{
    private String phone;
    private String name;
    private String password;
    private Package pack;
    private double consumerMoney;
    private double balance;
    private int realTall;
    private int realCount;
    private int realFlow;

    public User(String phone, String name, String password, Package pack, double consumerMoney, double balance) {
        this.phone = phone;
        this.name = name;
        this.password = password;
        this.pack = pack;
        this.consumerMoney = consumerMoney;
        this.balance = balance;
    }
    public  void print(){
        //精度
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("注册成功!");
        System.out.println("卡号:"+phone);
        System.out.println("姓名:"+name);
        System.out.println("消费金额:"+consumerMoney);
        System.out.println("当前余额:"+df.format(balance));
        pack.print();
    }
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Package getPack() {
        return pack;
    }

    public void setPack(Package pack) {
        this.pack = pack;
    }

    public double getConsumerMoney() {
        return consumerMoney;
    }

    public void setConsumerMoney(double consumerMoney) {
        this.consumerMoney = consumerMoney;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getRealTall() {
        return realTall;
    }

    public void setRealTall(int realTall) {
        this.realTall = realTall;
    }

    public int getRealCount() {
        return realCount;
    }

    public void setRealCount(int realCount) {
        this.realCount = realCount;
    }

    public int getRealFlow() {
        return realFlow;
    }

    public void setRealFlow(int realFlow) {
        this.realFlow = realFlow;
    }
}
//a.定义三个接口 通话接口(提供通话的方法)  短信接口(提供短信方法)   上网接口(提供上网方法)
interface CallService{
    void call();
}
interface SendService{
    void send();
}
interface NetService{
    void net();
}
//b.设计套餐对象的抽现象类具有(具有套餐价格属性, 打印信息方法)
//设计三个实现类 来继承抽象类
//话唠套餐类(可以通话和发短信 所以要实现通话和短信接口)
//超人套餐类(可以通话和发短信和使用流量 所以要实现通话 短信和上网接口)
//网虫套餐类(只能上网 只需要实现上网接口)
abstract class Package implements Serializable{//套餐
    double price;
    public abstract void print();
}
//话唠套餐
class TalkPackage extends Package{
    public TalkPackage(double price){
        super.price = price;
    }
    @Override
    public void print() {
        System.out.println("话痨套餐：价格"+price+"元");
    }

}
//超人套餐
class SuperPackage extends Package{
    public SuperPackage(double price){
        super.price = price;
    }
    @Override
    public void print() {
        System.out.println("超人套餐：价格"+price+"元");
    }
}
//网虫套餐
class NetPackage extends Package{
    public NetPackage(double price){
        super.price = price;
    }
    @Override
    public void print() {
        System.out.println("网虫套餐：价格"+price+"元");
    }
}
//d.实现注册功能 要求:
//选择卡号（提供以“139”开头的9个随机卡号,每行3个）
//选择套餐（话唠套餐、网虫套餐、超人套餐） 输入用户名、密码信息
//输入预存话费金额
//成功保存信息并提示 显示(卡号、用户名、当前余额、套餐信息)
class TestUser{
    //定义一个保存所以用户信息的集合
    static Map<String,User> map = new HashMap<>();
    static Scanner sc = new Scanner(System.in);
    static {
        input();
    }
    public static void main(String[] args) {
        menuOne();
    }
    public static void menuOne(){
        while (true){
            System.out.println("XXX用户系统");
            System.out.println("1.登录");
            System.out.println("1.注册");
            System.out.println("3.退出");
            System.out.println("请输入选择：");
            String choice = sc.next();
            switch (choice){
                case "1":login();break;
                case "2":register();break;
                case "3":return;
            }
        }
    }
    public static void login(){
        System.out.println("登录功能");
        for (String s :map.keySet()){
            map.get(s).print();
        }
    }
    public static void register(){
        String[] phones = randomPhones();
        for (int i = 0; i < phones.length; i++) {
            System.out.print((i+1)+"."+phones[i]+" ");
            if ((i+1)%3 == 0) System.out.println();
        }
        System.out.println("请选择手机号码:(1-9)");
        int choice1 = sc.nextInt();
        String phone = phones[choice1-1];
        System.out.println("请输入你需要选择的套餐:");
        System.out.println("1.话痨套餐 2.超人套餐 3.网虫套餐");
        int choice2 = sc.nextInt();
        Package pack = null;
        if (choice2 == 1) pack = new TalkPackage(50);
        else if (choice2 == 2)pack = new SuperPackage(88);
        else pack = new NetPackage(29.9);
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        System.out.println("请输入预存花费:");
        double money = 0;
        while (true){
            money = sc.nextDouble();
            if (money < pack.price){
                System.out.println("预存的花费不足以支付套餐费用,请重写充值");
            }else break;
        }
        User user = new User(phone,name,password,pack,pack.price,(money-pack.price));
        user.print();
        map.put(phone,user);
        //序列化
        output();
    }
    //反序列化
    public static void input(){
        try(FileInputStream fis = new FileInputStream("d://IO//a//map.User");
            ObjectInputStream ois = new ObjectInputStream(fis)
        ){
            map = (Map<String, User>) ois.readObject();
            System.out.println("用户信息获取成功");
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一次使用");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //序列化
    public static void output(){
        try(FileOutputStream fos = new FileOutputStream("d://IO//a//map.User");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(map);
            System.out.println("用户信息存储成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String[] randomPhones(){
        Set<String> set =  new HashSet<>();
        String start = "139";
        Random r = new Random();
        while (set.size()<9){
            for (int i = 0; i < 8; i++) {
                int number = r.nextInt(10);
                start+=number;
            }
            set.add(start);
            start = "139";
        }
        String[] phones = set.toArray(new String[0]);
        return phones;
    }
}